/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.wizards;

import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * This is the base class for all WizardPages of the Perforce Team Provider.
 *
 * @version $Revision: 494 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 * @author <a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
 */
abstract class PerforceWizardPage extends WizardPage {

  /** Stores the project (?) */
  protected IProject project;

  /**
   * Constructor for PerforceWizardPage.
   *
   * @param pageName the name of the page.
   */
  public PerforceWizardPage(String pageName) {
    super(pageName);
  }

  /**
   * Constructor for PerforceWizardPage.
   *
   * @param pageName the name of the page.
   * @param title the page's title.
   * @param titleImage descriptor of image shown in the title area.
   */
  public PerforceWizardPage(String pageName, String title, ImageDescriptor titleImage) {
    super(pageName, title, titleImage);
  }

  /**
   * Gets the project.
   *
   * @return Returns a IProject
   */
  public IProject getProject() {
    return project;
  }

  /**
   * Sets the project.
   *
   * @param project The project to set
   */
  public void setProject(IProject project) {
    this.project = project;
  }

  /**
   * Utility method that creates a Label instance and sets the default Layout
   * data.
   *
   * @param parent the parent composite.
   * @param labelText the label test.
   * @return the new Label.
   */
  static Label createLabel(Composite parent, String labelText) {
    Policy.assertNotNull(parent);
    Policy.assertNotNull(labelText);

    Label result = new Label(parent, SWT.WRAP);
    result.setText(labelText);

    return result;
  }

  /**
   * Utility method to create centered label.
   *
   * @param parent the parent composite.
   * @return the new (centered) Label.
   */
  static Label createCenteredLabel(Composite parent) {
    Policy.assertNotNull(parent);

    Label result = new Label(parent, SWT.WRAP | SWT.CENTER);
    return result;
  }

  /**
   * Utility method to create a label to show the given message.
   *
   * @param parent the parent composite.
   * @param message the message to display.
   * @return the new Label widget.
   */
  static Label createMessage(Composite parent, String message) {
    Policy.assertNotNull(parent);
    Policy.assertNotNull(message);

    Label result = new Label(parent, SWT.WRAP);
    result.setText(message);

    GridData data = new GridData();
    data.grabExcessVerticalSpace = true;
    data.widthHint = IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH;
    result.setLayoutData(data);

    return result;
  }

  /**
   * Utility method to create a combo box
   *
   * @param parent the parent of the combo box
   * @return the created combo
   */
  static Combo createCombo(Composite parent) {
    Policy.assertNotNull(parent);

    Combo result = new Combo(parent, SWT.READ_ONLY);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
    result.setLayoutData(data);

    return result;
  }

  /**
   * Utility method to create a Text field.
   *
   * @param parent the parent composite.
   * @return the new Text field.
   */
  static Text createText(Composite parent) {
    Policy.assertNotNull(parent);

    return createText(parent, 0, 0);
  }

  /**
   * Utility method to create a Text field, adding the given addStyle and
   * removing the specified removeStyle.
   *
   * @param parent the parent composite.
   * @param addStyle style to add.
   * @param removeStyle style to remove.
   * @return the new Text field.
   */
  static Text createText(Composite parent, int addStyle, int removeStyle) {
    Policy.assertNotNull(parent);

    Text result = new Text(parent, (SWT.SINGLE | SWT.BORDER | addStyle) & ~removeStyle);

    GridData data = new GridData(GridData.FILL_BOTH);
    data.widthHint = 350;
    data.heightHint = 50;
    result.setLayoutData(data);

    return result;
  }

  /**
   * Creates composite control and sets the default layout data.
   *
   * <br>
   * This code is originally taken from
   * <code>org.eclipse.team.internal.ccvs.ui.wizards.CVSWizardPage</code>.
   *
   * @param parent the parent of the new composite
   * @param numColumns the number of columns for the new composite
   * @return the newly-created composite
   */
  static Composite createComposite(Composite parent, int numColumns) {
    Policy.assertNotNull(parent);

    Composite result = new Composite(parent, SWT.NULL);

    // GridLayout
    GridLayout layout = new GridLayout();
    layout.numColumns = numColumns;
    result.setLayout(layout);

    // GridData
    GridData data = new GridData(GridData.FILL_BOTH);
    result.setLayoutData(data);

    return result;
  }

  /**
   * Create a text field specific for this application
   *
   * <br>
   * This code is originally taken from
   * <code>org.eclipse.team.internal.ccvs.ui.wizards.CVSWizardPage</code>.
   *
   * @param parent the parent of the new text field
   * @return the new text field
   */
  static Text createTextField(Composite parent) {
    Policy.assertNotNull(parent);

    Text result = new Text(parent, SWT.SINGLE | SWT.BORDER);

    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.verticalAlignment = GridData.CENTER;
    data.grabExcessVerticalSpace = false;
    data.grabExcessHorizontalSpace = true;
    data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
    result.setLayoutData(data);

    return result;
  }

  /**
   * Create a new Push button within the given parent and with the specified
   * label.
   *
   * @param parent the parent composite.
   * @param label the button's label.
   * @return the new Button instance.
   */
  static Button createButton(Composite parent, String label) {
    Policy.assertNotNull(parent);
    Policy.assertNotNull(label);

    Button button = new Button(parent, SWT.PUSH | SWT.BORDER);
    button.setText(label);

    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END);
    button.setLayoutData(data);

    return button;
  }
}
